package com.core.app.window.view.detail;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.vaadin.hezamu.googlemapwidget.GoogleMap;
import org.vaadin.hezamu.googlemapwidget.overlay.BasicMarker;
import org.vaadin.hezamu.googlemapwidget.overlay.PolyOverlay;

import com.core.app.event.CallbackEvent;
import com.core.app.window.view.data.DataFasilitasSekolahView;
import com.core.app.window.view.data.DataKelulusanView;
import com.core.app.window.view.data.DataPrestasiSekolahView;
import com.core.context.Inject;
import com.core.entity.FasilitasSekolah;
import com.core.entity.Kelulusan;
import com.core.entity.PrestasiSekolah;
import com.core.entity.Sekolah;
import com.core.service.FasilitasSekolahService;
import com.core.service.KelulusanService;
import com.core.service.PrestasiSekolahService;
import com.core.service.SekolahService;
import com.vaadin.annotations.AutoGenerated;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.Runo;

public class DetailSekolahView extends CustomComponent {

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	@AutoGenerated
	private VerticalLayout mainLayout;
	@AutoGenerated
	private HorizontalLayout horizontalLayout_1;
	@AutoGenerated
	private Button saveButton;
	@AutoGenerated
	private Button cancelButton;
	@AutoGenerated
	private Button viewMapButton;
	@AutoGenerated
	private TabSheet mainTab;
	@AutoGenerated
	private DataPrestasiSekolahView dataPrestasiSekolahView_1;
	@AutoGenerated
	private DataFasilitasSekolahView dataFasilitasSekolahView_1;
	@AutoGenerated
	private DataKelulusanView dataKelulusanView_1;
	@AutoGenerated
	private DetailInformasiDasarSekolahView informasiDasarView_1;
	private static final long serialVersionUID = 4701653797576546545L;
	private Sekolah sekolah;
	@Autowired
	private SekolahService sekolahService;
	@Autowired
	private PrestasiSekolahService prestasiSekolahService;
	@Autowired
	private FasilitasSekolahService fasilitasSekolahService;
	@Autowired
	private KelulusanService kelulusanService;
	private String tingkatan;
	private CallbackEvent loadEvents;
	private ClickListener saveClickListener = new ClickListener() {

		private static final long serialVersionUID = 4967160436328059848L;

		@Override
		public void buttonClick(ClickEvent event) {
			try {
				sekolah = informasiDasarView_1.getSekolah();
				sekolah.setKelulusan(new ArrayList<Kelulusan>(
						dataKelulusanView_1.getKelulusanSekolah().values()));
				sekolah.setFasilitasSekolah(new ArrayList<FasilitasSekolah>(
						dataFasilitasSekolahView_1.getFasilitasSekolah()
								.values()));
				sekolah.setPrestasiSekolahs(dataPrestasiSekolahView_1
						.getPrestasiSekolahs());
				sekolah.setTingkatan(getTingkatan());
				sekolahService.saveOrUpdate(sekolah);
				getApplication().getMainWindow().removeWindow(getWindow());
				loadEvents.callback(null);
			} catch (Exception e) {
				e.printStackTrace();
				getParent().getWindow().showNotification(e.getMessage());
			}

		}
	};

	private void viewLocationOnMap() {
		Window mapWindow = new Window(sekolah.getNama() + " ("
				+ sekolah.getLatitude() + "," + sekolah.getLongitude() + ")");
		mapWindow.setWidth("500px");
		mapWindow.setHeight("300px");
		mapWindow.setModal(true);
		GoogleMap googleMap = new GoogleMap(getApplication());
		googleMap.setCenter(new Point2D.Double(new Double(sekolah
				.getLongitude()), new Double(sekolah.getLatitude())));
		googleMap.setWidth("100%");
		googleMap.setHeight("100%");

		BasicMarker basicMarker = new BasicMarker(1L, googleMap.getCenter(),
				sekolah.getNama());
		basicMarker.setDraggable(false);
		googleMap.addMarker(basicMarker);
		HorizontalLayout layout = new HorizontalLayout();
		layout.addComponent(googleMap);
		layout.setSizeFull();
		mapWindow.setContent(layout);
		getApplication().getMainWindow().addWindow(mapWindow);
	}

	/**
	 * The constructor should first build the main layout, set the composition
	 * root and then do any custom initialization.
	 * 
	 * The constructor will not be automatically regenerated by the visual
	 * editor.
	 * 
	 * @throws Exception
	 */
	public DetailSekolahView(Sekolah sekolah) throws Exception {
		Inject.Inject(this);
		buildMainLayout();
		setCompositionRoot(mainLayout);
		setSekolah(sekolah);
		mainTab.addStyleName(Runo.TABSHEET_SMALL);
		saveButton.addListener(saveClickListener);
		cancelButton.addListener(new ClickListener() {

			private static final long serialVersionUID = -7406381761854169441L;

			@Override
			public void buttonClick(ClickEvent event) {
				DetailSekolahView.this.getApplication().getMainWindow()
						.removeWindow(getWindow());
			}
		});
		viewMapButton.addListener(new ClickListener() {

			private static final long serialVersionUID = 3206911437995761515L;

			@Override
			public void buttonClick(ClickEvent event) {
				viewLocationOnMap();
			}
		});
	}

	public CallbackEvent getLoadEvents() {
		return loadEvents;
	}

	public void setLoadEvents(CallbackEvent loadEvents) {
		this.loadEvents = loadEvents;
	}

	public String getTingkatan() {
		return this.tingkatan;
	}

	public void setTingkatan(String tingkatan) {
		this.tingkatan = tingkatan;
		informasiDasarView_1.getMetodeTextArea().setVisible(
				"TK".equals(tingkatan) ? true : false);
	}

	public Sekolah getSekolah() {
		return sekolah;
	}

	public void setSekolah(Sekolah sekolah) throws Exception {
		this.sekolah = sekolah;

		// menampilkan informasi dasar sekolah
		informasiDasarView_1.setSekolah(sekolah);
		if ("TK".equals(tingkatan)) {
			informasiDasarView_1.getMetodeTextArea().setVisible(false);
		} else {
			informasiDasarView_1.getMetodeTextArea().setVisible(true);
		}

		// menampilkan kelulusan sekolah
		List<Kelulusan> kelulusanSekolah = new ArrayList<Kelulusan>();
		Map<String, Kelulusan> kelMap = new HashMap<String, Kelulusan>();
		if (sekolah.getId() != null) {
			kelulusanSekolah = kelulusanService.kelulusanSekolah(sekolah);
		}
		for (Kelulusan kelulusan : kelulusanSekolah) {
			kelMap.put(kelulusan.getTahunAjaran(), kelulusan);
		}
		dataKelulusanView_1.setKelulusanSekolah(kelMap);
		dataKelulusanView_1.setSekolah(sekolah);

		// menampilkan fasilitas sekolah
		List<FasilitasSekolah> fasilitasSekolah = new ArrayList<FasilitasSekolah>();
		if (sekolah.getId() != null) {
			fasilitasSekolah = fasilitasSekolahService
					.fasilitasSekolah(sekolah);
		}
		Map<String, FasilitasSekolah> fSSMap = new HashMap<String, FasilitasSekolah>();
		for (FasilitasSekolah fSS : fasilitasSekolah) {
			fSSMap.put(fSS.getFasilitas().getNama(), fSS);
		}
		dataFasilitasSekolahView_1.setFasilitasSekolah(fSSMap);
		dataFasilitasSekolahView_1.setSekolah(sekolah);

		List<PrestasiSekolah> prestasiSekolahs = new ArrayList<PrestasiSekolah>();
		prestasiSekolahs
				.addAll((Collection<? extends PrestasiSekolah>) (sekolah
						.getId() != null ? prestasiSekolahService
						.prestasiSekolah(sekolah) : Collections.emptyList()));
		dataPrestasiSekolahView_1.setPrestasiSekolahs(prestasiSekolahs);
		dataPrestasiSekolahView_1.setSekolah(sekolah);

	}

	@AutoGenerated
	private VerticalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("750px");
		mainLayout.setHeight("-1px");
		mainLayout.setMargin(false);

		// top-level component properties
		setWidth("750px");
		setHeight("-1px");

		// mainTab
		mainTab = buildMainTab();
		mainLayout.addComponent(mainTab);
		mainLayout.setExpandRatio(mainTab, 1.0f);

		// horizontalLayout_1
		horizontalLayout_1 = buildHorizontalLayout_1();
		mainLayout.addComponent(horizontalLayout_1);
		mainLayout.setComponentAlignment(horizontalLayout_1, new Alignment(34));

		return mainLayout;
	}

	@AutoGenerated
	private TabSheet buildMainTab() {
		// common part: create layout
		mainTab = new TabSheet();
		mainTab.setImmediate(true);
		mainTab.setWidth("100.0%");
		mainTab.setHeight("100.0%");

		// informasiDasarView_1
		informasiDasarView_1 = new DetailInformasiDasarSekolahView();
		informasiDasarView_1.setImmediate(false);
		informasiDasarView_1.setWidth("100.0%");
		informasiDasarView_1.setHeight("100.0%");
		mainTab.addTab(informasiDasarView_1, "Informasi Dasar", null);

		// dataKelulusanView_1
		dataKelulusanView_1 = new DataKelulusanView();
		dataKelulusanView_1.setImmediate(false);
		dataKelulusanView_1.setWidth("100.0%");
		dataKelulusanView_1.setHeight("100.0%");
		mainTab.addTab(dataKelulusanView_1, "Kelulusan", null);

		// dataFasilitasSekolahView_1
		dataFasilitasSekolahView_1 = new DataFasilitasSekolahView();
		dataFasilitasSekolahView_1.setImmediate(false);
		dataFasilitasSekolahView_1.setWidth("100.0%");
		dataFasilitasSekolahView_1.setHeight("100.0%");
		mainTab.addTab(dataFasilitasSekolahView_1, "Fasilitas", null);

		// dataPrestasiSekolahView_1
		dataPrestasiSekolahView_1 = new DataPrestasiSekolahView();
		dataPrestasiSekolahView_1.setImmediate(false);
		dataPrestasiSekolahView_1.setWidth("100.0%");
		dataPrestasiSekolahView_1.setHeight("-1px");
		mainTab.addTab(dataPrestasiSekolahView_1, "Prestasi", null);

		return mainTab;
	}

	@AutoGenerated
	private HorizontalLayout buildHorizontalLayout_1() {
		// common part: create layout
		horizontalLayout_1 = new HorizontalLayout();
		horizontalLayout_1.setImmediate(false);
		horizontalLayout_1.setWidth("-1px");
		horizontalLayout_1.setHeight("-1px");
		horizontalLayout_1.setMargin(true);
		horizontalLayout_1.setSpacing(true);

		// viewMapButton
		viewMapButton = new Button();
		viewMapButton.setCaption("View On Map");
		viewMapButton.setImmediate(true);
		viewMapButton.setWidth("-1px");
		viewMapButton.setHeight("-1px");
		horizontalLayout_1.addComponent(viewMapButton);

		// cancelButton
		cancelButton = new Button();
		cancelButton.setCaption("Cancel");
		cancelButton.setImmediate(true);
		cancelButton.setWidth("100px");
		cancelButton.setHeight("-1px");
		horizontalLayout_1.addComponent(cancelButton);

		// saveButton
		saveButton = new Button();
		saveButton.setCaption("Save");
		saveButton.setImmediate(true);
		saveButton.setWidth("100px");
		saveButton.setHeight("-1px");
		horizontalLayout_1.addComponent(saveButton);
		horizontalLayout_1.setComponentAlignment(saveButton, new Alignment(34));

		return horizontalLayout_1;
	}

}
